package com.yfqy.admin.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yfqy.admin.constant.MyConstants;
import com.yfqy.admin.domain.dto.attribute.AddAttribute;
import com.yfqy.admin.domain.dto.attribute.AttributeDTO;
import com.yfqy.admin.domain.dto.attribute.AttributeQuery;
import com.yfqy.admin.domain.dto.attribute.ModifiedAttribute;
import com.yfqy.admin.domain.entity.Attribute;
import com.yfqy.admin.domain.mapper.AttributeMapper;
import com.yfqy.admin.transform.UserNameTranslator;
import com.yfqy.admin.security.login.LoginUserInfo;
import com.yfqy.admin.service.AttributeService;
import com.yfqy.admin.util.AuthUserContext;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

@Service
public class AttributeServiceImpl implements AttributeService {

    @Resource
    private AttributeMapper attributeMapper;
    @Resource
    private UserNameTranslator userNameTranslator;

    @Override
    public int deleteByPrimaryKey(Long id) {
        return attributeMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int insert(Attribute record) {
        return attributeMapper.insert(record);
    }

    @Override
    public int insertSelective(Attribute record) {
        return attributeMapper.insertSelective(record);
    }

    @Override
    public AttributeDTO selectByPrimaryKey(Long id) {
        Attribute attribute = attributeMapper.selectByPrimaryKey(id);
        return BeanUtil.toBean(attribute, AttributeDTO.class);
    }

    @Override
    public int updateByPrimaryKeySelective(Attribute record) {
        return attributeMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public int updateByPrimaryKey(Attribute record) {
        return attributeMapper.updateByPrimaryKey(record);
    }

    @Override
    public PageInfo<AttributeDTO> pageListByNameAndType(AttributeQuery attributeQuery) {
        PageHelper.startPage(attributeQuery.getPageIndex(), attributeQuery.getPageSize());
        List<AttributeDTO> attributes = attributeMapper.selectListByNameAndType(attributeQuery.getName(), attributeQuery.getType());
        if (CollUtil.isNotEmpty(attributes)) {
            userNameTranslator.translationUser(attributes);
        }
        PageInfo<AttributeDTO> pageInfo = new PageInfo<>(attributes);
        PageHelper.clearPage();
        return pageInfo;
    }

    @Override
    public int addAttribute(AddAttribute addAttribute) {
        // 1-输入框 2-单选 3-多选
        if (addAttribute.getInputType() != MyConstants.BT1 && StringUtils.isBlank(addAttribute.getValues())) {
            throw new IllegalArgumentException("属性预定义值列表不能为空");
        }
        Attribute attribute = attributeMapper.selectOneByName(addAttribute.getName());
        if (Objects.nonNull(attribute)) {
            throw new IllegalArgumentException("属性已存在");
        }
        Attribute bean = BeanUtil.toBean(addAttribute, Attribute.class);
        LoginUserInfo loginUserInfo = AuthUserContext.getLocalAuthUser();
        Long createdBy = Objects.nonNull(loginUserInfo) ? loginUserInfo.getId() : MyConstants._L1;
        bean.setCreatedBy(createdBy);
        bean.setModifiedBy(createdBy);
        return attributeMapper.insertSelective(bean);
    }

    @Override
    public int modifyAttribute(ModifiedAttribute modifyAttribute) {
        // 1-输入框 2-单选 3-多选
        if (modifyAttribute.getInputType() != MyConstants.BT1 && StringUtils.isBlank(modifyAttribute.getValues())) {
            throw new IllegalArgumentException("属性预定义值列表不能为空");
        }
        Attribute attributeById = attributeMapper.selectByPrimaryKey(modifyAttribute.getId());
        if (Objects.isNull(attributeById)) {
            throw new IllegalArgumentException("属性id不存在");
        }
        Attribute attributeByName = attributeMapper.selectOneByName(modifyAttribute.getName());
        if (Objects.nonNull(attributeByName) && !attributeByName.getId().equals(attributeById.getId())) {
            throw new IllegalArgumentException("属性已存在，不可重复添加");
        }
        BeanUtil.copyProperties(modifyAttribute, attributeById, CopyOptions.create().setIgnoreNullValue(true));
        LoginUserInfo loginUserInfo = AuthUserContext.getLocalAuthUser();
        Long modifiedBy = Objects.nonNull(loginUserInfo) ? loginUserInfo.getId() : MyConstants._L1;
        attributeById.setModifiedBy(modifiedBy);
        return attributeMapper.updateByPrimaryKeySelective(attributeById);
    }

}
